#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, b, a) for (int i = (b); i >= (a); i--)
#define N 3030

vector<int> p[N];
map<vector<int>, int> cnt;
bool visr[N], visc[N];
int n, m, row[N], col[N];
int ans;
void solve() {
  cnt.clear();
  int mx = 0;
  rep(i, 0, n - 1) mx = max(mx, ++cnt[p[i]]);
  ans = min(ans, n + m - mx);
}
bool able() {
  rep(i, 0, n - 1) rep(j, 0, m - 1) row[i] += p[i][j], col[j] += p[i][j];
  int r = n, c = m;
  while (r > 0 && c > 0) {
    bool flag = false;
    rep(i, 0, n - 1) if (!visr[i] && (row[i] == 0 || row[i] == c)) {
      flag = visr[i] = true, r--;
      rep(j, 0, m - 1) col[j] -= p[i][j];
    }
    if (flag)
      continue;
    rep(j, 0, m - 1) if (!visc[j] && (col[j] == 0 || col[j] == r)) {
      flag = visc[j] = true, c--;
      rep(i, 0, n - 1) row[i] -= p[i][j];
    }
    if (!flag)
      return false;
  }
  return true;
}
int main() {
  cin >> n >> m;

  rep(i, 0, max(n, m) - 1) p[i].resize(max(n, m));
  rep(i, 0, n - 1) rep(j, 0, m - 1) {
    char ch;
    cin >> ch;
    p[i][j] = (ch == 'R');
  }

  if (!able()) {
    puts("-1");
    return 0;
  }
  ans = n + m;
  solve();

  rep(i, 0, n - 1) rep(j, i, m - 1) swap(p[i][j], p[j][i]);
  swap(n, m);
  solve();

  cout << ans << endl;
  return 0;
}
